<?php
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $settings, $user_info;
global $dbtables;

// Check for an admin log view call.
$adminlog = (isset($_GET['adminlog'])) ? $_GET['adminlog'] : '';

if (!empty($adminlog))
{
	$player = $_POST['player'];
	
	//
	// DOUBLECHECK - this is not valid with the new user system
	// 
	/*
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE email = {string:player}
		LIMIT 1',
		array(
			'db_ships' => $dbtables['ships'],
			'player' => $player
		)
	);
	$playerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
	*/
}
else
{
	// Verify user login, if CheckLogin() returns false return to the calling script/function
	if (CheckLogin() == false)
		return;

	bigtitle($txt['bnt_title_log']);

	// Pull player info into $playerinfo.
	$result = $smcFunc['db_query']('', '
		SELECT *
		FROM {raw:db_ships}
		WHERE user_id = {int:user_id}
		LIMIT 1',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$playerinfo = $smcFunc['db_fetch_assoc']($result);
	$smcFunc['db_free_result']($result);
}

echo '
		<center>
		<table width="80%" border="0" cellspacing="0" cellpadding="0">';

$logline = str_replace("[player]", "$playerinfo[character_name]", $txt['bnt_log_log']);

echo '
		<tr>
			<td><td width=100%><td>
		</tr>
		<tr>
			<td></td>
			<td height=20 style="background-image: url(', $settings['images_url'], '/bnt/top_panel.gif); background-repeat:no-repeat">
				<font size=2 color=#040658 align="left"><b>&nbsp;&nbsp;&nbsp;', $logline, '</b></font>
			</td>
			<td></td>
			<td>&nbsp;</td>
		</tr>
		<tr>
			<td valign=bottom>
				<td colspan=2 bgcolor=#63639C>';



$startdate = (isset($_GET['startdate'])) ? $_GET['startdate'] : '';
				
if (empty($startdate))
  $startdate = date("Y-m-d");

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_logs}
	WHERE ship_id = {int:ship_id}
	AND time LIKE {string:startdate}
	ORDER BY time DESC, type DESC',
	array(
		'db_logs' => $dbtables['logs'],
		'ship_id' => $playerinfo['ship_id'],
		'startdate' => $startdate . '%'
	)
);
  
while ($row = $smcFunc['db_fetch_assoc']($result))
{
	$logs[] = $row;
}
$smcFunc['db_free_result']($result);

$entry = $txt['bnt_log_months'][date("m", strtotime($startdate)) - 1] . " " . date("d", strtotime($startdate)) . " " . date("Y", strtotime($startdate));

echo '		
	<div id="divScroller1">
		<div id="dynPage0" class="dynPage">
		<center>
		<br>
		<font size=2 color=#DEDEEF><b>', $txt['bnt_log_start'], ' ', $entry, '<b></font>
		<p>
		<hr width=80% size=1 NOSHADE style="color: #040658">
		</center>';

if (!empty($logs))
{
	foreach($logs as $log)
	{
		$event = log_parse($log);
		$time = $txt['bnt_log_months'][date("m", strtotime($log['time'])) - 1] . " " . date("d", strtotime($log['time'])) . " " . date("Y", strtotime($log['time'])) . " " . substr($log['time'], 11);

		echo '	
		<table border=0 cellspacing=5 width=100%>
		<tr>
		<td><font size=2 color=#040658><b>', $event['title'], '</b></td>
		<td align=right><font size=2 color=#040658><b>', $time, '</b></td>
		<tr><td colspan=2>
		<font size=2 color=#DEDEEF>
		', $event['text'], '
		</td></tr>
		</table>';
	}
}

echo '
	</div>';	

$nextlink = date("Y-m-d", strtotime("+1 days",strtotime($startdate)));
$date1 = date("Y-m-d", strtotime("-1 days",strtotime($startdate)));
$date2 = date("Y-m-d", strtotime("-2 days",strtotime($startdate)));
$date3 = date("Y-m-d", strtotime("-3 days",strtotime($startdate)));
$backlink = date("Y-m-d", strtotime("-4 days",strtotime($startdate)));

echo '
</div>';

$displaydate1 = $txt['bnt_log_months_short'][date("m", strtotime($date1)) - 1] . " " . date("d", strtotime($date1));
$displaydate2 = $txt['bnt_log_months_short'][date("m", strtotime($date2)) - 1] . " " . date("d", strtotime($date2));
$displaydate3 = $txt['bnt_log_months_short'][date("m", strtotime($date3)) - 1] . " " . date("d", strtotime($date3));

if ($nextlink > date("Y-m-d"))
	$nonext = 1;
else
	$nonext = 0;

//fix for admin log view
if (!empty($adminlog))
	$postlink = ";adminlog;player=$player";
else
	$postlink = "";

echo '
<tr><td><td align=right>
<a href=', $scripturl, '?action=blacknova;sa=log;startdate=', $backlink . $postlink, '><font color=white size=3><b><<</b></font></a>&nbsp;&nbsp;&nbsp;
<a href=', $scripturl, '?action=blacknova;sa=log;startdate=', $date3 . $postlink, '><font color=white size=3><b>', $displaydate3, '</b></font></a>
&nbsp;|&nbsp;
<a href=', $scripturl, '?action=blacknova;sa=log;startdate=', $date2 . $postlink, '><font color=white size=3><b>', $displaydate2, '</b></font></a>
 | 
<a href=', $scripturl, '?action=blacknova;sa=log;startdate=', $date1 . $postlink, '><font color=white size=3><b>', $displaydate1, '</b></font></a>
	';

if ($nonext != 1)
	echo '&nbsp;&nbsp;&nbsp;<a href=', $scripturl, '?action=blacknova;sa=log;startdate=', $nextlink . $postlink, '><font color=white size=3><b>>></b></font></a>';

echo '&nbsp;&nbsp;&nbsp;';

echo '</table>
     </center>';

function log_parse($entry)
{
	global $txt;
	global $space_plague_kills;

	switch ($entry['type'])
	{
		case LOG_LOGIN: //data args are : [ip]
		case LOG_LOGOUT:
		case LOG_BADLOGIN:
		case LOG_HARAKIRI:
			$retvalue['text'] = str_replace("[ip]", "<font color=white><b>$entry[data]</b></font>", $txt['bnt_log_text'][$entry['type']]);
			$retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
			continue;

		case LOG_ATTACK_OUTMAN: //data args are : [player]
		case LOG_ATTACK_OUTSCAN:
		case LOG_ATTACK_EWD:
		case LOG_ATTACK_EWDFAIL:
		case LOG_SHIP_SCAN:
		case LOG_SHIP_SCAN_FAIL:
		case LOG_XENOBE_ATTACK:
		case LOG_TEAM_NOT_LEAVE:
			$retvalue['text'] = str_replace("[player]", "<font color=white><b>$entry[data]</b></font>", $txt['bnt_log_text'][$entry['type']]);
			$retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
			continue;

	    case LOG_ATTACK_LOSE: //data args are : [player] [pod]
			list($name,$pod) = split ("\|", $entry['data']);

			$retvalue['text'] = str_replace("[player]", "<font color=white><b>$name</b></font>", $txt['bnt_log_text'][$entry['type']]);
			$retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
			if ($pod == 'Y')
				$retvalue['text'] = $retvalue['text'] . $txt['bnt_log_pod'];
			else
				$retvalue['text'] = $retvalue['text'] . $txt['bnt_log_nopod'];
			continue;

		case LOG_ATTACKED_WIN: //data args are : [player] [armor] [fighters]
			list($name, $armor, $fighters)= split ("\|", $entry['data']);
			$retvalue['text'] = str_replace("[player]", "<font color=white><b>$name</b></font>", $txt['bnt_log_text'][$entry['type']]);
			$retvalue['text'] = str_replace("[armor]", "<font color=white><b>$armor</b></font>", $retvalue['text']);
			$retvalue['text'] = str_replace("[fighters]", "<font color=white><b>$fighters</b></font>", $retvalue['text']);
			$retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
			continue;

	case LOG_TOLL_PAID: //data args are : [toll] [sector]
	case LOG_TOLL_RECV:
		list($toll, $sector)= split ("\|", $entry['data']);
		$retvalue['text'] = str_replace("[toll]", "<font color=white><b>$toll</b></font>", $txt['bnt_log_text'][$entry['type']]);
		$retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $retvalue['text']);
		$retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
		continue;

    case LOG_HIT_MINES: //data args are : [mines] [sector]
	    list($mines, $sector)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[mines]", "<font color=white><b>$mines</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_SHIP_DESTROYED_MINES: //data args are : [sector] [pod]
    case LOG_DEFS_KABOOM:
	    list($sector, $pod)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    if($pod == 'Y')
			$retvalue['text'] = $retvalue['text'] . $txt['bnt_log_pod'];
	    else
			$retvalue['text'] = $retvalue['text'] . $txt['bnt_log_nopod'];
	    continue;

    case LOG_PLANET_DEFEATED_D: //data args are :[planet_name] [sector] [name]
    case LOG_PLANET_DEFEATED:
    case LOG_PLANET_SCAN:
    case LOG_PLANET_SCAN_FAIL:
    list($planet_name, $sector, $name)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[planet_name]", "<font color=white><b>$planet_name</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[name]", "<font color=white><b>$name</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_PLANET_NOT_DEFEATED: //data args are : [planet_name] [sector] [name] [ore] [organics] [goods] [salvage] [credits]
    list($planet_name, $sector, $name, $ore, $organics, $goods, $salvage, $credits)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[planet_name]", "<font color=white><b>$planet_name</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[name]", "<font color=white><b>$name</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[ore]", "<font color=white><b>$ore</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[goods]", "<font color=white><b>$goods</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[organics]", "<font color=white><b>$organics</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[salvage]", "<font color=white><b>$salvage</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[credits]", "<font color=white><b>$credits</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_RAW: //data is stored as a message
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    $retvalue['text'] = $entry['data'];
	    continue;

    case LOG_DEFS_DESTROYED: //data args are : [quantity] [type] [sector]
	    list($quantity, $type, $sector)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[quantity]", "<font color=white><b>$quantity</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[type]", "<font color=white><b>$type</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_PLANET_EJECT: //data args are : [sector] [player]
	    list($sector, $name)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[name]", "<font color=white><b>$name</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_STARVATION: //data args are : [sector] [starvation]
	    list($sector, $starvation)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[starvation]", "<font color=white><b>$starvation</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_TOW: //data args are : [sector] [newsector] [hull]
		list($sector, $newsector, $hull)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[newsector]", "<font color=white><b>$newsector</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[hull]", "<font color=white><b>$hull</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_DEFS_DESTROYED_F: //data args are : [fighters] [sector]
	    list($fighters, $sector)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[fighters]", "<font color=white><b>$fighters</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_TEAM_REJECT: //data args are : [player] [teamname]
	    list($player, $teamname)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[player]", "<font color=white><b>$player</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[teamname]", "<font color=white><b>$teamname</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_TEAM_RENAME: //data args are : [team]
    case LOG_TEAM_M_RENAME:
    case LOG_TEAM_KICK:
    case LOG_TEAM_CREATE:
    case LOG_TEAM_LEAVE:
    case LOG_TEAM_LEAD:
    case LOG_TEAM_JOIN:
    case LOG_TEAM_INVITE:
	    $retvalue['text'] = str_replace("[team]", "<font color=white><b>$entry[data]</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_TEAM_NEWLEAD: //data args are : [team] [name]
    case LOG_TEAM_NEWMEMBER:
	    list($team, $name)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[team]", "<font color=white><b>$team</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[name]", "<font color=white><b>$name</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_ADMIN_HARAKIRI: //data args are : [player] [ip]
	    list($player, $_SERVER['REMOTE_ADDR'])= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[player]", "<font color=white><b>$player</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[ip]", "<font color=white><b>$_SERVER[REMOTE_ADDR]</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_ADMIN_ILLEGVALUE: //data args are : [player] [quantity] [type] [holds]
	    list($player, $quantity, $type, $holds)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[player]", "<font color=white><b>$player</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[quantity]", "<font color=white><b>$quantity</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[type]", "<font color=white><b>$type</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[holds]", "<font color=white><b>$holds</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_ADMIN_PLANETDEL: //data args are : [attacker] [defender] [sector]
	    list($attacker, $defender, $sector)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[attacker]", "<font color=white><b>$attacker</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[defender]", "<font color=white><b>$defender</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_DEFENCE_DEGRADE: //data args are : [sector] [degrade]
	    list($sector, $degrade)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[degrade]", "<font color=white><b>$degrade</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;

    case LOG_PLANET_CAPTURED: //data args are : [cols] [credits] [owner]
	    list($cols, $credits, $owner)= split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[cols]", "<font color=white><b>$cols</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[credits]", "<font color=white><b>$credits</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[owner]", "<font color=white><b>$owner</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;
	
    case LOG_BOUNTY_CLAIMED:
	    list($amount,$bounty_on,$placed_by) = split ("\|", $entry['data']);
	    $retvalue['text'] = str_replace("[amount]", "<font color=white><b>$amount</b></font>", $txt['bnt_log_text'][$entry['type']]);
	    $retvalue['text'] = str_replace("[bounty_on]", "<font color=white><b>$bounty_on</b></font>", $retvalue['text']);
	    $retvalue['text'] = str_replace("[placed_by]", "<font color=white><b>$placed_by</b></font>", $retvalue['text']);
	    $retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
	    continue;
		
	case LOG_BOUNTY_PAID:
		list($amount,$bounty_on) = split ("\|", $entry['data']);
		$retvalue['text'] = str_replace("[amount]", "<font color=white><b>$amount</b></font>", $txt['bnt_log_text'][$entry['type']]);
		$retvalue['text'] = str_replace("[bounty_on]", "<font color=white><b>$bounty_on</b></font>", $retvalue['text']);
		$retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
		continue;
		
	case LOG_BOUNTY_CANCELLED:
		list($amount,$bounty_on) = split ("\|", $entry['data']);
		$retvalue['text'] = str_replace("[amount]", "<font color=white><b>$amount</b></font>", $txt['bnt_log_text'][$entry['type']]);
		$retvalue['text'] = str_replace("[bounty_on]", "<font color=white><b>$bounty_on</b></font>", $retvalue['text']);
		$retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
		continue;
		
	case LOG_BOUNTY_FEDBOUNTY:
		$retvalue['text'] = str_replace("[amount]", "<font color=white><b>$entry[data]</b></font>", $txt['bnt_log_text'][$entry['type']]);
		$retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
		continue;
	
	case LOG_SPACE_PLAGUE:
		list($name,$sector) = split ("\|", $entry['data']);
		$retvalue['text'] = str_replace("[name]", "<font color=white><b>$name</b></font>", $txt['bnt_log_text'][$entry['type']]);
		$retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $retvalue['text']);
		$percentage = $space_plague_kills * 100;
		$retvalue['text'] = str_replace("[percentage]", "$space_plague_kills", $retvalue['text']);
		$retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
		continue;
		
	case LOG_PLASMA_STORM:
		list($name,$sector) = split ("\|", $entry['data']);
		$retvalue['text'] = str_replace("[name]", "<font color=white><b>$name</b></font>", $txt['bnt_log_text'][$entry['type']]);
		$retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $retvalue['text']);
		$retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
		continue;
	
	case LOG_PLANET_BOMBED:
		list($planet_name, $sector, $name, $beams, $torps, $figs)= split ("\|", $entry['data']);
		$retvalue['text'] = str_replace("[planet_name]", "<font color=white><b>$planet_name</b></font>", $txt['bnt_log_text'][$entry['type']]);
		$retvalue['text'] = str_replace("[sector]", "<font color=white><b>$sector</b></font>", $retvalue['text']);
		$retvalue['text'] = str_replace("[name]", "<font color=white><b>$name</b></font>", $retvalue['text']);
		$retvalue['text'] = str_replace("[beams]", "<font color=white><b>$beams</b></font>", $retvalue['text']);
		$retvalue['text'] = str_replace("[torps]", "<font color=white><b>$torps</b></font>", $retvalue['text']);
		$retvalue['text'] = str_replace("[figs]", "<font color=white><b>$figs</b></font>", $retvalue['text']);
		$retvalue['title'] = $txt['bnt_log_title'][$entry['type']];
		continue;

	}
	return $retvalue;
}

?>

